package model;

import java.util.ArrayList;
import checking.IdGenerator;


public class Project {

	private Integer number;
	private String name = null;
	private String desc = null;
	private String startDate = null;
	private ArrayList<Activity> activityList = new ArrayList<Activity>();
	private ProjectManager projectManager = null;
	private int pmId;
	
	IdGenerator oId = new IdGenerator();

	public Project(int id,String name, String desc, String startDate, int pManager) {
		this.number=id;
		this.name = name;
		this.desc = desc;
		this.startDate = startDate;
		this.pmId = pManager;
	}	
	
	public Project(){
		
	}
	// getter and setter methods...
	public ArrayList<Activity> getActivityList() {
		return activityList;
	}

	public void setActivityList(ArrayList<Activity> activityList) {
		this.activityList = activityList;
	}

	public ProjectManager getProjectManager() {
		return projectManager;
	}

	public void setProjectManager(ProjectManager projectManager) {
		this.projectManager = projectManager;
	}
	
	public ArrayList<Activity> getActivity() {
		return activityList;
	}

	public void setActivity(ArrayList<Activity> activity) {
		this.activityList = activity;
	}

	public String getName() {
		return name;
	}
	
	public int getPmId() {
		return pmId;
	}

	public void setPmId(int pmId) {
		this.pmId = pmId;
	}

	// it sets the name of the project if it is not null
	public void setName(String name) {
	 try {
		 if (!(name.equals(""))){
			this.name = name;
		}
		
	} catch (NullPointerException e) {
			System.out.println("Please enter a valid name!"+e.getMessage());
			
	}
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		try {
			 if (!(desc.equals(""))){
				this.desc = desc;
			}
			
		} catch (NullPointerException e) {
				System.out.println("Please enter a valid description!"+e.getMessage());
				
		}
			
	}

	public String getStartDate() {
		return startDate;
	}

	
	// input validation for date is making just after getting date from user  
	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public void setNumber(Integer number) {
		if (number >0){
		this.number = number;}
		
		else throw new IllegalArgumentException("Your value " + number + "is not valid!");
	}

	public Integer getNumber() {
		return number;
	}
	
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Project other = (Project) obj;
		if (activityList == null) {
			if (other.activityList != null)
				return false;
		} else if (!activityList.equals(other.activityList))
			return false;
		if (desc == null) {
			if (other.desc != null)
				return false;
		} else if (!desc.equals(other.desc))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (number == null) {
			if (other.number != null)
				return false;
		} else if (!number.equals(other.number))
			return false;
		if (oId == null) {
			if (other.oId != null)
				return false;
		} else if (!oId.equals(other.oId))
			return false;
		if (projectManager == null) {
			if (other.projectManager != null)
				return false;
		} else if (!projectManager.equals(other.projectManager))
			return false;
		if (startDate == null) {
			if (other.startDate != null)
				return false;
		} else if (!startDate.equals(other.startDate))
			return false;
		return true;
	}
	// addActivity method...
	public Activity addActivity(String actDesc, String actStartDate, String actDeliverable) {
		Activity oActivity = new Activity(oId.generateId(),actDesc, actStartDate, actDeliverable);
		activityList.add(oActivity);
		return oActivity;
	}

	// removeActivity method...
	public void removeActivity(int actId) {
		
		for (int i = 0; i < activityList.size(); i++) {
			if (activityList.get(i).getNumber().equals(actId)){
				for(int j=0; j<activityList.get(i).getoTaskList().size(); j++){
					activityList.get(i).getoTaskList().remove(j);//if you delete one activity from a project you will delete its tasks automatically
				}
				activityList.remove(i);
			}
		}
	}

	// updateActivity method...
	public Activity updateActivity(int actId, String desc, String startDate,
			int estimatedHours, String deliverable) {
		Activity updatedAct = null;
		updatedAct = findActivity(actId);
				updatedAct.setDesc(desc);
				updatedAct.setStartDate(startDate);// start date icin input validation !!
				updatedAct.setEstimatedHours(estimatedHours);
				updatedAct.setDeliverable(deliverable);
		return updatedAct;
	}


public Activity findActivity(int actId)
{
	Activity foundActivity=null;
	
	for(int i=0;i<activityList.size();i++){		
	if(activityList.get(i).getNumber() == actId){
		foundActivity = activityList.get(i);
										}
	}
	
	return foundActivity;
}

public void displayActivities(){
	System.out.println("Your activity list of this project is;\n");
	for(int i=0; i<activityList.size(); i++){
		System.out.println(activityList.get(i).toString()+"\n");
	}
}

}
